import { defineStore } from "pinia";
import { ElMessage } from 'element-plus';
import { reqCheckStatus, reqLoginKey, reqLoginKeyImg, reqLoginStatus,reqUserFollows,reqUserEvent,reqUserFolloweds,reqLogout,reqUserMessage } from "@/api/login";
let loginStore = defineStore('loginStore', {
    state: () => {
        return {
            flag:false,
            expire:false,
            loginKey:"",
            base_64Url:'',
            timer:undefined,
            cookie:localStorage.getItem('cookie'),
            sqStatus:false,
            loginStatus:false,
            account:null,
            profile:null,
            userFollow:[],
            userEvent:[],
            userFollowsed:[],
            userMessage:[],
            loginFlag:'login3',
            loginTermFlag:false,
            loginIphone:'',
            loginCode:''
        }
    },
    actions:{
        async getLoginStatus (cookie:string | null){
            let result = await reqLoginStatus(cookie)
            if(result.data.account!==null&&result.data.profile!==null){
                this.loginStatus = true
                this.account = result.data.account
                this.profile = result.data.profile
                if(this.loginStatus){
                    let resultEvent = await reqUserEvent(this.profile.userId)
                    this.userEvent = resultEvent
                    let resultFollows = await reqUserFollows(this.profile.userId)
                    this.userFollow = resultFollows.follow
                    let resultFollowsed = await reqUserFolloweds(this.profile.userId)
                    this.userFollowsed = resultFollowsed
                    let resultMessage = await reqUserMessage(3)
                    this.userMessage = resultMessage
                }
            }
        },
        async getRwmLogin() {
            let result = await reqLoginKey();
            if (result.code == 200) {
                this.loginKey = result.data.unikey;
                let result2 = await reqLoginKeyImg(this.loginKey)
                if (result2.code == 200) {
                    this.base_64Url = result2.data.qrimg;
                    this.timer = setInterval(async () => {
                        const statusRes = await reqCheckStatus(this.loginKey)
                        if (statusRes.code === 800) {
                            this.expire=true
                            this.sqStatus=false
                            clearInterval(this.timer)
                        }
                        if (statusRes.code === 803) {
                            // 这一步会返回cookie
                            clearInterval(this.timer)
                            this.flag = false
                            this.sqStatus=false
                            await this.getLoginStatus(statusRes.cookie)
                            localStorage.setItem('cookie', statusRes.cookie)
                        }
                        if(statusRes.code ===802){
                            this.sqStatus=true
                        }
                    }, 3000)
                }
            }
        },
        refresh(){
            this.expire=false
            this.getRwmLogin()
        },
        getLoginFlag(loginmessage:string){
            if(this.loginTermFlag){
                this.loginFlag=loginmessage
            }else{
                ElMessage({
                    type:"info",
                    message:'请先勾选同意《服务条款》、《隐私政策》、《儿童隐私政策》',
                })
            }
        },
        async getLoginOut(){
            let result = await reqLogout()
            if(result.code==200){
                this.loginStatus = false
                this.cookie = null
                localStorage.removeItem('cookie')
            }
        }
    }
})

export default loginStore